What is cli-width?
The cli-width package is a simple utility that allows you to retrieve the current width of the command line interface (CLI) in which your node.js application is running. It is useful for creating CLI tools that need to format output according to the width of the terminal window.
What are cli-width's main functionalities?
Get CLI width
This feature allows you to get the current width of the CLI. The function returns a number representing the number of columns in the terminal, or a default value if it can't be determined.
const cliWidth = require('cli-width');
console.log(cliWidth());
Set default width
This feature allows you to set a default width that cli-width will return if it cannot determine the actual width of the terminal. This is useful for ensuring your application has a fallback width to use for formatting output.
const cliWidth = require('cli-width');
cliWidth.defaultWidth = 100;
console.log(cliWidth());
Other packages similar to cli-width
window-size
The window-size package is similar to cli-width in that it provides the dimensions of the terminal window. However, window-size offers both width and height properties, and it can also provide dimensions based on environment variables or stdout/stderr dimensions.
term-size
Term-size is another package that offers similar functionality to cli-width. It provides the size of the terminal as an object with 'columns' and 'rows'. Unlike cli-width, term-size uses a native addon to retrieve terminal dimensions, which may offer more accurate results in some environments.
cli-width
Get stdout window width, with three fallbacks, tty
, a custom environment variable and then a default.
Usage
npm install --save cli-width
'use stict';
var cliWidth = require('cli-width');
cliWidth();
You can also set the CLI_WIDTH
environment variable.
If none of the methods are supported, and the environment variable isn't set,
the default is 0
and can be changed via cliWidth.defaultWidth = 200;
.
Tests
npm install
npm test
Coverage can be generated with npm run coverage
.